/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author tuan
 */
public class CategoryDAL {
    
    public static void insert(String productId, String productName, String categoryId, float unitPrice, int unitsInStock, int unitsInOrder, String productImage, String description) throws ClassNotFoundException, SQLException {
        Connection con = null;
        PreparedStatement psmt = null;
        String sql = "insert into Products(ProductID, ProductName, CategoryID, UnitPrice,"
                + " UnitsInStock, UnitsInOrder, ProductImage, Description)" + "values(?,?,?,?,?,?,?,?)";
        try {
            con = ConnectionUtitility.openConnection();
            psmt = con.prepareStatement(sql);
            psmt.setString(1, productId);
            psmt.setString(2, productName);
            psmt.setString(3, categoryId);
            psmt.setFloat(4, unitPrice);
            psmt.setInt(5, unitsInStock);
            psmt.setInt(6, unitsInOrder);
            psmt.setString(7, productImage);
            psmt.setString(7, description);
            psmt.executeUpdate();
        } finally {
            try {
                psmt.close();
                con.close();
            } catch (Exception e) {
            }
        }
    }
    //checkLogin function to check whether the username and password are existed in database

   
    public static Collection<CategoryEntity> searchList(String name) throws ClassNotFoundException, SQLException {
        Collection<CategoryEntity> list = null;
        Connection con = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        String sql = "select * from Products where ProductName like '%"+name+"%'";
        try {
            con = ConnectionUtitility.openConnection();
            psmt = con.prepareStatement(sql);
            rs = psmt.executeQuery();
            list = new ArrayList<CategoryEntity>();
            while (rs.next()) {
                CategoryEntity entity = new CategoryEntity();
                
                
                
                
                list.add(entity);
            }
        } finally {
            try {
                psmt.close();
                con.close();
            } catch (Exception e) {
            }
        }
        return list;
    }
    
    //Function to list all employees
    public static Collection<CategoryEntity> listAll() throws ClassNotFoundException, SQLException {
        Collection<CategoryEntity> list = null;
        Connection con = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        String sql = "select * from [SMS].[dbo].[Categories]";
        try {
            con = ConnectionUtitility.openConnection();
            psmt = con.prepareStatement(sql);
            rs = psmt.executeQuery();
            list = new ArrayList<CategoryEntity>();
            while (rs.next()) {
                CategoryEntity entity = new CategoryEntity();
                entity.setCategoryID(rs.getString("CategoryID"));
                entity.setCategoryName(rs.getString("CategoryName"));
                list.add(entity);
            }
        } finally {
            try {
                psmt.close();
                con.close();
            } catch (Exception e) {
            }
        }
        return list;
    }
}
